package com.easywan.service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.easywan.dao.GtdStepMapper;
import com.easywan.dao.GtdTrashMapper;
import com.easywan.dao.IGtdDao;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.pojo.PageRow;
import com.easywan.util.DateUtils;
import com.easywan.vo.Gtd;
import com.easywan.vo.GtdStep;
import com.easywan.vo.GtdTrash;

import junit.framework.Assert;

public class GtdService implements IGtdService{
	private static Logger logger = Logger.getLogger(GtdService.class);
	@Autowired
	IGtdDao gtdDao;
	@Autowired
	GtdStepMapper stepMapper;
	@Autowired
	GtdTrashMapper trashMapper;
	@Override
	public IResult getUsersGtd(Integer usersId) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Gtd gtd = new Gtd();
		gtd.setUid(usersId);
		gtd.setStatus("T");
		List<Gtd> gtds = gtdDao.selectBy(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, gtds);
	}

	@Override
	public IResult getUsersDoingGtd(Integer usersId, String doing) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Gtd gtd = new Gtd();
		gtd.setUid(usersId);
		gtd.setDoing(doing);
		List<Gtd> gtds = gtdDao.selectBy(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, gtds);
	}

	@Override
	public IResult getUsersDoneGtd(Integer usersId, String done) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Gtd gtd = new Gtd();
		gtd.setUid(usersId);
		gtd.setDone(done);
		List<Gtd> gtds = gtdDao.selectBy(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, gtds);
	}

	@Override
	public IResult getUsersGoneGtd(Integer usersId, String gone) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Gtd gtd = new Gtd();
		gtd.setUid(usersId);
		gtd.setGone(gone);
		List<Gtd> gtds = gtdDao.selectBy(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, gtds);
	}

	@Override
	public IResult getUsersRabitGtd(Integer usersId, String status) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Gtd gtd = new Gtd();
		gtd.setUid(usersId);
		gtd.setStatus(status);
		List<Gtd> gtds = gtdDao.selectBy(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, gtds);
	}

	@Override
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult addGtd(String name, String content, Integer uid,String startDate,String endDate) {
		Gtd gtd = new Gtd();
		gtd.setUid(uid);
		gtd.setContent(content);
		gtd.setName(name);
		Assert.assertNotNull(name);
		
		Date createtime = new Date();
		gtd.setCreatetime(createtime);
		gtd.setModifytime(createtime);
			gtd.setStartdate(DateUtils.StringToDate(startDate,"yyyy-MM-dd HH:mm"));
			gtd.setEnddate(DateUtils.StringToDate(endDate,"yyyy-MM-dd HH:mm"));
		
		
		int i =  gtdDao.insertSelective(gtd);
		return IResultSupport.buildResult(IResult.SUCCESS_CODE,"添加成功");
	}

	@Override
	public Gtd getByUsersIdAndId(Integer usersId, Integer id) {
		Gtd gtd = gtdDao.selectByPrimaryKey(id);
		if(gtd.getUid().equals(usersId))
			return gtd;
		else{
			return null;
		}
	}

	@Override
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult modifyGtd(String name, String content, Integer uid, Integer id,String startDate,String endDate) {
		Gtd gtd = new Gtd();
		gtd.setId(id);
		gtd.setName(name);
		gtd.setContent(content);
		gtd.setUid(uid);
		gtd.setModifytime(new Date());
		gtd.setStartdate(DateUtils.StringToDate(startDate,"yyyy-MM-dd HH:mm"));
		gtd.setEnddate(DateUtils.StringToDate(endDate,"yyyy-MM-dd HH:mm"));
		logger.info("修改内容："+gtd);
		int i = gtdDao.updateByPrimaryKeySelective(gtd);
		if(i <= 0){
			return IResultSupport.buildResult(IResult.FAILT_CODE, "修改失败");
		}
		return IResultSupport.buildResult(IResult.SUCCESS_CODE, "修改成功");
	}

	@Override
	public IResult getGtdStep(Integer gid, Integer uid,PageRow pageRow) {
		GtdStep record = new GtdStep();
		record.setUid(uid);
		record.setGid(gid);
		record.setPage(pageRow);
		List<GtdStep> steps = stepMapper.selectByPage(record);
		
		return IResultSupport.buildSuccessResult(steps, pageRow);
	}

	@Override
	public IResult addGtdStep(Integer gid, Integer uid, String name, String bief,String startDate,String endDate) {
		GtdStep step = new GtdStep();
		step.setBief(bief);
		step.setGid(gid);
		step.setUid(uid);
		step.setName(name);
		Date date = new Date();
		step.setCreatetime(date);
		step.setModifytime(date);
		
		step.setStartdate(DateUtils.StringToDate(startDate,"yyyy-MM-dd HH:mm"));
		step.setEnddate(DateUtils.StringToDate(endDate,"yyyy-MM-dd HH:mm"));
		
		
		int i = stepMapper.insertSelective(step);
		if(i>0){
			return IResultSupport.buildSuccessResult("添加成功");
		}else{
			return IResultSupport.buildFailtResult("添加失败");
		}
	}

	@Override
	public IResult getUsersGtd(Integer usersId, PageRow pageRow) {
		if(usersId == null){
			throw new RuntimeException("用户ID异常！");
		}
		Map<String,Object> gtd = new HashMap<>();
		gtd.put("uid",usersId);
		gtd.put("status","T");
		gtd.put(PageRow.PAGE_CONST, pageRow);
		List<Map<String,Object>> gtds = gtdDao.selectByPage(gtd);
		return IResultSupport.buildSuccessResult( gtds,pageRow);
	}

	@Override
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult delToTrash(Integer usersId, Integer id) {
		Assert.assertNotNull(usersId);
		Assert.assertNotNull(id);
		Gtd updateGtd = new Gtd();
		updateGtd.setUid(usersId);
		updateGtd.setId(id);
		//如果无法找到 则说明没有权限
		Gtd selectGtd = gtdDao.selectBy(updateGtd).get(0);
		
		//插入trash
		GtdTrash trash = new GtdTrash();
		trash.setUid(usersId);
		trash.setBief(selectGtd.getContent());
		Date date = new Date();
		trash.setCreatetime(date);
		trash.setModifytime(date);
		trash.setName(selectGtd.getName());
		trashMapper.insertSelective(trash);
		
		updateGtd.setStatus("F");
		
		gtdDao.updateByPrimaryKeySelective(updateGtd);
		
		return IResultSupport.buildSuccessResult("加入垃圾箱成功！");
	}

	@Override
	public IResult delStepToTrash(Integer usersId, Integer id) {
		Assert.assertNotNull(usersId);
		Assert.assertNotNull(id);
		GtdStep updateGtdStep = new GtdStep();
		updateGtdStep.setUid(usersId);
		updateGtdStep.setId(id);
		//如果无法找到 则说明没有权限
		GtdStep selectGtd = stepMapper.selectBy(updateGtdStep).get(0);
		
		//插入trash
		GtdTrash trash = new GtdTrash();
		trash.setUid(usersId);
		trash.setBief(selectGtd.getBief());
		Date date = new Date();
		trash.setCreatetime(date);
		trash.setModifytime(date);
		trash.setName(selectGtd.getName());
		trash.setTypeName(GtdTrash.STEP);
		trashMapper.insertSelective(trash);
		
		//updateGtdStep.setStatus("F");
		
		//gtdDao.updateByPrimaryKeySelective(updateGtdStep);
		stepMapper.deleteByPrimaryKey(id);
		
		return IResultSupport.buildSuccessResult("加入垃圾箱成功！");
	}

	@Override
	public GtdStep getStepByUsersIdAndId(Integer usersId, Integer id) {
		GtdStep step= stepMapper.selectByPrimaryKey(id);
		if(step.getUid().equals(usersId))
			return step;
		else{
			return null;
		}
	}

	@Override
	@Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public IResult toFinish(Integer usersId, Integer id) {
		Gtd record = new Gtd();
		record.setId(id);
		record.setUid(usersId);
		record.setModifytime(new Date());
		record.setDone("T");
		
		int rst = gtdDao.updateByPrimaryKeySelective(record );
		
		if(rst > 0){
			return IResultSupport.buildSuccessResult("修改成功！");
		}
		
		return IResultSupport.buildFailtResult("修改失败！");
	}

	@Override
	public IResult toStart(Integer usersId, Integer id) {
		Gtd record = new Gtd();
		record.setId(id);
		record.setUid(usersId);
		record.setModifytime(new Date());
		record.setDoing("T");
		
		int rst = gtdDao.updateByPrimaryKeySelective(record );
		
		if(rst > 0){
			return IResultSupport.buildSuccessResult("开始成功！");
		}
		
		return IResultSupport.buildFailtResult("开始失败！");
	}

}
